<template>
<el-config-provider :locale="locale">
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>护士信息管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card >
      <!-- 搜索与添加区域   -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getNurseList" >
            <template #append>
              <el-button  :icon="Search" @click="getNurseList"/>
            </template>
          </el-input>
        </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true">添加用户</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="nurseList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"  />
        <el-table-column prop="NurseName" label="姓名"  />
        <el-table-column prop="NurseSex" label="性别"  />
        <el-table-column prop="NurseAge" label="年龄"  />
        <el-table-column prop="NurseTelNum" label="电话" />
        <el-table-column prop="Position" label="职位"  />
        <el-table-column prop="CovidTest_img" label="核酸检测图片" />
        <el-table-column  label="操作" >
          <!-- scope是一个插槽 -->
          <template #default="scope">  
            <!-- 编辑按钮 -->
            <el-button size="small" @click="showEditDialog( scope.row.id)">编辑</el-button>
            <!-- 删除按钮 -->
            <el-button size="small" type="danger" @click="deleteById( scope.row.id)" >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination  v-model:currentPage="queryInfo.pagenum" v-model:page-size="queryInfo.pagesize"
      :page-sizes="[2, 3, 5, 10]" :small="small" :disabled="disabled" :background="background"
      layout="total, sizes, prev, pager, next, jumper" :total="total"
      @size-change="handleSizeChange"  @current-change="handleCurrentChange" />
       
    </el-card>

    <!--  添加用户对话框 --> 
    <el-dialog v-model="addDialogVisible" title="添加护士" width="50%" @close="addDialogClosed">
      <!-- 表单内容区域 -->
      <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="90px"  :size="formSize">
        <el-form-item label="用户名" prop="NurseName">
          <el-input v-model="addForm.NurseName" />
        </el-form-item>
        <el-form-item label="性别" prop="NurseSex">
          <el-select  v-model="addForm.NurseSex" clearable placeholder="请选择性别" style="width:100%">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="NurseAge">
          <el-input v-model="addForm.NurseAge"/>
        </el-form-item>
        <el-form-item label="电话" prop="NurseTelNum">
          <el-input v-model="addFormNurseTelNum" />
        </el-form-item>
        <el-form-item label="职位" prop="Position">
          <el-input v-model="addForm.Position"/>
        </el-form-item>
        <el-form-item label="核酸检测图片" prop="CovidTest_img">
          <el-input v-model="addForm.CovidTest_img" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addNurse">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 修改用户对话框 -->
    <el-dialog v-model="editDialogVisible" title="编辑用户" width="50%" @close="editDialogClosed">
      <!-- 表格内容主体区域 -->
      <el-form ref="editFormRef" :model="editForm" :rules="editFormRules" label-width="80px"  :size="formSize">
        <el-form-item label="用户名" >
          <el-input v-model="editForm.NurseName"  disabled/>
        </el-form-item>
        <el-form-item label="性别" >
          <el-input v-model="editForm.NurseSex"/>
        </el-form-item>
        <el-form-item label="年龄" >
          <el-input v-model="editForm.NurseAge"/>
        </el-form-item>
        <el-form-item label="电话" prop="NurseTelNum">
          <el-input v-model="editForm.NurseTelNum" />
        </el-form-item>
        <el-form-item label="职位" prop="Position">
          <el-input v-model="editForm.Position"/>
        </el-form-item>
        <el-form-item label="核酸检测图片" prop="CovidTest_img">
          <el-input v-model="editForm.CovidTest_img" />
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editNurseInfo">确定</el-button>
        </span>
      </template>
    </el-dialog>


  </div>
  </el-config-provider>
</template>

<script>
import { ElConfigProvider } from 'element-plus'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import { ArrowRight } from '@element-plus/icons-vue'
import { Search } from '@element-plus/icons-vue'
import qs from 'qs'
import {  ElMessageBox } from 'element-plus'
export default {
    components:{
    ArrowRight,
    ElConfigProvider,
    ElMessageBox

  },
  setup(){
    return{
      Search,
      locale: zhCn
    }
  },
    data(){
        //验证手机号的规则
        var checkTelNum=(rule,value,cb)=>{
          //验证手机号的正则表达式
          const regTelNum=/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
          if(regTelNum.test(value)) {
            return cb()
           }
          cb(new Error('请输入合法的手机号'))
        }
        return{
            //获取用户列表的参数对象
            queryInfo:{
               query:'',
               //当前页数
               pagenum:1,
               //当前每页显示多少调数据
               pagesize:2,
            },
            //所有用户的数据列表
            nurseList:[],
            total:0,
            //控制添加用户对话框的显示与隐藏
            addDialogVisible:false,
            //添加用户的表单数据
            addForm:{
               NurseName:'',
               NurseSex:'',
               NurseAge:'',
               NurseTelNum:'',
               Position:'',
               CovidTest_img:'',
            },
            //添加表单的验证规则对象
            addFormRules:{
                NurseName:[{
                  required:true,message:'请输入名字',trigger:'blur'
                },{ min:2,max:5,message:'名字在2~5个字符之间' ,trigger:'blur'}],
                NurseSex:[{
                  required:true,message:'请选择性别',trigger:'blur'
                }],
                NurseAge:[{
                   required:true,message:'请输入年龄',trigger:'blur'
                }],
                NurseTelNum:[{
                   required:true,message:'请输入电话',trigger:'blur'
                },{validator:checkTelNum,trigger:'blur'}],
                Position:[{
                   required:true,message:'请输入职位',trigger:'blur'
                }],
                CovidTest_img:[{
                  required:true,message:'请上传核酸检测图片',trigger:'blur'
                }],
            },
            //控制编辑用户对话框的显示与隐藏
            editDialogVisible:false,
            //查询到用户的信息对象
            editForm:{},
            //修改表单的验证规则对象
            editFormRules:{
              NurseTelNum:[{
                required:true,message:'请输入电话',trigger:'blur'
              },{validator:checkTelNum,trigger:'blur'}],
              Position:[{
                required:true,message:'请输入职位',trigger:'blur'
              }],
              CovidTest_img:[{
                required:true,message:'请上传核酸检测图片',trigger:'blur'
              }],
      
            },
            

        }

    },

    created(){
        //函数
        this.getNurseList()

    },

    methods:{
        async getNurseList(){
            const {data:res} =await this.$http.get('/my/nursemessage/getnurse',{params:this.queryInfo})
            if(res.meta.status!==200) {
              return this.$message.err('获取护士信息列表失败')
            }
            this.nurseList=res.data.nurse
            this.total=res.data.total
            console.log(res)
        },
        //监听pagesize改变的事件
        handleSizeChange(newSize){
          //console.log(newSize)
          this.queryInfo.pagesize=newSize
          this.getNurseList()
        },
        //监听页码值改变的事件
        handleCurrentChange(newPage){
          console.log(newPage)
          this.queryInfo.pagenum=newPage
          this.getNurseList()
        },
        //监听对话框的关闭事件resetFields()
        addDialogClosed(){
           this.$refs.addFormRef.resetFields()
        },
        //添加新用户
        addPatient(){
          this.$refs.addFormRef.validate(async valid=>{
          if(!valid)  return 
          //可以发出添加用户的网络请求
          const {data:res} =await this.$http.post('/my/nursemessage/addnurse',qs.stringify(this.addForm))
          //console.log(res)
          if(res.meta.status!==201){
            this.$message.Error('添加用户失败')
        }
           this.$message.success('添加用户成功')
           //隐藏用户对话框
           this.addDialogVisible=false
          //重新获取用户列表数据
          this.getNurseList()

        })
        },
        //展示编辑的对话框
        async showEditDialog(id){
            //console.log(id)
            const {data:res} =await this.$http.get('/my/nursemessage/getnursebyid/'+id)
            console.log(res)
            if(res.meta.status!==200) { 
               return this.$message.error('查询护士信息失败')
            }
            this.editForm=res.data
            this.editDialogVisible=true

        },
        //监听修改用户对话框的关闭事件
        editDialogClosed(){
            this.$refs.editFormRef.resetFields()
        },
        //修改用户信息并提交
        editNurseInfo(){
            this.$refs.editFormRef.validate( async valid=>{
                //console.log(valid)
                //发起修改用户信息的数据请求
                var data = qs.stringify(this.editForm)
                const {data:res} = await this.$http.post('/my/nursemessage/updatenurse/'+ this.editForm.id, data,
                {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
                
                //判断请求是否成功
                if (res.meta.status !== 200) {
                  return this.$message.error('更新护士信息失败！')
                }
                //关闭对话框
                this.editDialogVisible=false
                //刷新数据列表
                this.getNurseList()
                //提示修改成功
                this.$message.success('更新护士信息成功！')

            })
        },
        //根据id删除用户信息
        async deleteById(id){
           //console.log(id)
           //弹框提示是否删除数据
           const confirmResult=await ElMessageBox.confirm('此操作将永久删除该用户，是否继续?','提示',
           {
               confirmButtonText: '确定',
               cancelButtonText: '取消',
               type: 'warning',
            }).catch(err=>err)
            //如果用户确认删除，返回字符串confirm
            //console.log(confirmResult)
            //如果用户取消了删除，返回值为字符串cancel
            if(confirmResult!=='confirm'){
                return  this.$message.info('已取消删除')
            }
            const {data:res} =await this.$http.get('/my/nursemessage/deletenurse/'+id)
            if (res.meta.status !== 200) {
                return this.$message.error('删除信息失败！')
            }
            this.$message.success('删除信息成功！')
        },
    },



}
</script>

<style lang="less" scoped>

</style>